package robotter.bluecambot;

import robotter.bluecambot.R;
import robotter.bluecambot.camera_handle;
import robotter.bluecambot.communication;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.LinearLayout;


public class bluecambot extends Activity {
	camera_handle cam_mng=null;
	communication comm=null;
	int frame_id=0;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        /* Initialisation de la communication */
//        comm=new communication(this);
//        while(!comm.connected()){
//        	if (comm.initConnection()==-1){
//        		return;
//        	}
//        }
        

        /* Initialisation de la caméra */
        cam_mng=new camera_handle(this, comm);
        Log.v("bluecambot","Camera created");
        
        

        
    }
    
    @Override
    public void onDestroy() {
    	if (cam_mng!=null){
    		cam_mng.finalize();
    	}
    	if (comm!=null){
    		comm.finalize();
    	}
    	super.onDestroy();
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
    	if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0) {
            LinearLayout top_panel=(LinearLayout)findViewById(R.id.top_panel);
            LinearLayout bottom_panel=(LinearLayout)findViewById(R.id.bottom_panel);
            LinearLayout middle_panel=(LinearLayout)findViewById(R.id.middle_panel);
            switch (frame_id){
	            case 0:
	                Log.v("bluecambot","Switch to frame 0");
	                bottom_panel.setVisibility(LinearLayout.GONE);
	                middle_panel.setVisibility(LinearLayout.GONE);
	                top_panel.setVisibility(LinearLayout.VISIBLE);
	                frame_id=1;
	                break;
	            case 1:
	                Log.v("bluecambot","Switch to frame 1");
	                top_panel.setVisibility(LinearLayout.GONE);
	                middle_panel.setVisibility(LinearLayout.GONE);
	                bottom_panel.setVisibility(LinearLayout.VISIBLE);
	                frame_id=2;
	                break;
	            case 2:
	                Log.v("bluecambot","Switch to frame 2");
	                top_panel.setVisibility(LinearLayout.GONE);
	                bottom_panel.setVisibility(LinearLayout.GONE);
	                middle_panel.setVisibility(LinearLayout.VISIBLE);
	                frame_id=0;
	                break;
	            default:
	            	frame_id=0;
                	break;
            }

    	}
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            return super.onKeyDown(keyCode, event);
        }

        return true;
    }
    
    
}